package com.company.bs2024;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JianFeng Zhang(张建锋)
 * @date 2024-03-30 14:58:23
 */
public class FulltextRetrieval {


    public static void main(String[] args) {
        FulltextRetrieval func = new FulltextRetrieval();
        //String[] ret = func.query(new String[]{"石家庄", "老石营"}, "石家营");
        String[] ret = func.query(new String[]{"1石1营1", "1石营11"}, "石营");
        for (String s : ret) {
            System.out.println(s);
        }
    }

    public static int compar(SerchResult r1, SerchResult r2) {
        System.out.println(r1.toString());
        System.out.println(r2.toString());
        int r = r1.machNum - r2.machNum;
        if (r == 0) {
            r = r2.itemLen - r1.itemLen;
            if (r == 0) {
                r = r1.machStr.length() - r2.machStr.length();
                if (r == 0) {
                    r = r1.posSum - r2.posSum;
                    if (r == 0) {
                        r = r2.index - r1.index;
                    }
                }
            }
        }

        return r;
    }

    public String[] query(String[] texts, String keyword) {
        String ret[] = new String[texts.length];
        List<SerchResult> a = new ArrayList<>();
        for (int i = 0; i < texts.length; i++) {
            a.add(serchItem(texts[i], i, keyword));
        }
        return a.stream().sorted(FulltextRetrieval::compar).map(SerchResult::getstring).
                collect(Collectors.toList()).toArray(new String[0]);
    }

    public SerchResult serchItem(String text, int ind, String keyword) {
        SerchResult rt = new SerchResult();
        rt.itemLen = text.length();
        rt.txt = text;
        rt.index = ind;
        int lastpos = -1;
        for (int i = 0; i < keyword.length(); i++) {
            String key = keyword.substring(i, i + 1);
            int pos = text.indexOf(key);
            if (pos > -1) {
                rt.machNum++;
                rt.posSum += pos;
                rt.keyWords += ("".equals(rt.keyWords) ? key : "," + key);
                if (lastpos == -1) {
                    rt.machStr = key;
                } else {
                    //看是否相邻，相邻不加空格
                    if (lastpos == (pos - 1)) {
                        rt.machStr += key;
                    } else {
                        rt.machStr += " " + key;
                    }
                }
                lastpos = pos;
                rt.searchFlag = true;
            }
        }
        return rt;
    }

    private class SerchResult {
        //1)包含字最多的条目排在前面;同一字多次匹配只计数1次
        int machNum = 0;
        //2)如果包含字数相同，条目短的排在前面
        int itemLen = 0;
        //3)如果包含字数相同且条目长度相同，条目中包含字的起止位置两边内容裁 剪掉，包含各个字之间的全部内
        // 形成匹配浓缩内容，匹配 浓缩内容短的条目排在前面。
        String machStr = "";
        //4)如果按照上述 3 条规则计算后位置相同的条目，则针对各个条目，计算匹 配关键字在搜索关键词中首位量
        int posSum = 0;
        //5)如果不符合上述 4条规则，需与数据条目列表的顺序保持一致
        int index = -1;
        //条目内容
        String txt;
        //配到的关键列表字,符串的 构成格式是"数据条目,关键字 1,关键字 2
        String keyWords = "";
        //是否配到。
        boolean searchFlag = false;

        public String getstring() {
            return txt + "," + keyWords;
        }

        @Override
        public String toString() {
            return "SerchResult{" +
                    "machNum=" + machNum +
                    ", itemLen=" + itemLen +
                    ", machStr='" + machStr + '\'' +
                    ", posSum=" + posSum +
                    ", index=" + index +
                    ", txt='" + txt + '\'' +
                    ", keyWords='" + keyWords + '\'' +
                    ", searchFlag=" + searchFlag +
                    '}';
        }
    }

}
